/**
 * 
 */
package name.marz.to.go.btree.ui.swing;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;

import name.marz.to.go.btree.node.Node;


/**
 * @author masaru-gotou
 *
 */
public class NodeDrawer {
	private Graphics g;
	private Font font;
	private GraphicsConfigration gc = GraphicsConfigration.getInstance();

	public NodeDrawer(Graphics g,GraphicsConfigration gc) {
		this.g = g;
		this.gc = gc;
	}
	public NodeDrawer(Graphics g) {
		this.g = g;
	}
	
	public void draw(Point topcenter, Node node) {
		int pointerw = gc.getPointerWidth();
		int keyw = gc.getKeyWidth();
		Dimension d = gc.getBounds(node);
		int nodeh = (int)d.getHeight();
		int nodew = (int)d.getWidth();
		
		Point topleft = new Point(topcenter);
		topleft.translate((int)-nodew/2, 0);
		// 箱(枠)を描画
		g.setColor(Color.white);
		g.fillRect(topleft.x, topleft.y, nodew, nodeh );
		g.setColor(Color.black);
		g.drawRect(topleft.x, topleft.y, nodew, nodeh );
		// 左ポインタの線を描画
//		g.setColor(Color.black);
//		g.drawLine(topleft.x+pointerw, topleft.y, topleft.x+pointerw, topleft.y+nodeh);
		// キー、ポインタの線とキーの中味を描画
		int x = topleft.x+ pointerw;
		for ( int i=0; i<node.getMaxKeys(); i++ ) {
			g.setFont(font);
			if ( node.getKey(i) != null ) {
				g.drawString(String.valueOf(node.getKey(i).getValue()), x+2, topleft.y+12);
			}
			x += keyw;
			g.drawLine(x, topleft.y, x, topleft.y+nodeh);
//			x += pointerw;
//			g.drawLine(x, topleft.y, x, topleft.y+nodeh);
		}
	}
}
